﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SenerPro;

namespace MensUnoShop.Models
{
    public class DBSaleskit
    {
        private DataClassesMenDataContext _dc;
        private Logger _log = new Logger(Config.logpath);

        #region 生日折扣相關
        public bool GetBirthdayDiscount(Guid mguid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var m = _dc.UBirthdayDiscount.SingleOrDefault(od => od.BDMguid == mguid && od.BDdatein.Year == DateTime.Today.Year && od.BDdatein.Month == DateTime.Today.Month);
                    if (m != null)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetBirthdayDiscount", ex.ToString());
            }
            return true;
        }

        public bool SetBirthdayDiscount(Guid mguid, Guid oguid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    UBirthdayDiscount bd = new UBirthdayDiscount();
                    bd.BDdatein = DateTime.Now;
                    bd.BDMguid = mguid;
                    bd.BDOguid = oguid;
                    _dc.UBirthdayDiscount.InsertOnSubmit(bd);
                    _dc.SubmitChanges();
                }
                return true;
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetBirthdayDiscount", ex.ToString());
            }
            return false;
        }

        public void DelBirthdayDiscount(string txid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var o = _dc.UOrders.SingleOrDefault(oo => oo.Otxid == txid);
                    if (o != null)
                    {
                        string sql = "DELETE FROM UBirthdayDiscount WHERE BDMguid='"+o.OMguid+"' AND BDOguid='"+o.Oguid+"'";
                        _dc.ExecuteCommand(sql);
                        _dc.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "DelBirthdayDiscount", ex.ToString());
            }
        }
        #endregion

        public bool SetNewCoupon(string title, string coupon,int price)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sc = _dc.USaleskitCoupon.SingleOrDefault(s => s.SCticket == coupon);
                    if (sc != null)
                        return false;
                    else
                    {
                        sc = new USaleskitCoupon();
                        sc.SCdatein = DateTime.Now;
                        sc.SCprice = price;
                        sc.SCtitle = title;
                        sc.SCticket = coupon;
                        _dc.USaleskitCoupon.InsertOnSubmit(sc);
                        _dc.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetNewCoupon", ex.ToString());
            }
            return false;
        }

        public List<V_CouponList> GetCouponDiscount()
        {
            List<V_CouponList> clist = new List<V_CouponList>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sc = _dc.V_CouponList;
                    if (sc.Count() > 0)
                        return sc.ToList();
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetNewCoupon", ex.ToString());
            }
            return clist;
        }


        public List<USalesKit> GetManagerSalesKitList(Config.SalesKitKind salesKitKind)
        {
            List<USalesKit> clist = new List<USalesKit>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sc = _dc.USalesKit.Where(s => s.SSkind == (int)salesKitKind);
                    if (sc!=null)
                        return sc.ToList();
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerSalesKitList", ex.ToString());
            }
            return clist;
        }

        public string AddManagerSalesKit(string stitle, string tagids, string note, DateTime st, DateTime et, Config.SalesKitRulesType salesKitRulesType, Config.SalesKitType salesKitType, Config.SalesKitKind salesKitKind, ref Guid id)
        {
            string msg = string.Empty;
            
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    bool ischeck = true;
                    var sc = _dc.USalesKit.Where(s => s.SSkind == (int)salesKitKind && s.SSrtype==(int)salesKitRulesType && s.SStype==(int)salesKitType);
                    if (sc != null)
                    { 
                        //檢查日期有沒有重覆
                        if (sc.Any(s => s.SSkind == (int)Config.SalesKitKind.HanselDiscount &&
                            ((s.SSstartdate < st && s.SSenddate > et) ||
                                (s.SSstartdate > st && s.SSenddate > et && s.SSstartdate<et) ||
                                (s.SSstartdate < st && s.SSenddate < et && s.SSenddate>st) ||
                                (s.SSstartdate > st && s.SSenddate < et)
                            )
                           ))
                        {
                            id = Guid.Empty;
                            msg = "活動區間重覆!!";
                            ischeck = false;
                        }
                        else if (sc.Any(s => s.SStitle == stitle))
                        {
                            id = Guid.Empty;
                            msg = "活動名稱重覆!!";
                            ischeck = false;
                        }
                    }
                    if(ischeck)
                    {
                        id = Guid.NewGuid();
                        USalesKit sk = new USalesKit();
                        sk.SSdatein = DateTime.Now;
                        sk.SSenddate = et;
                        sk.SSguid = id;
                        sk.SSkind = (int)salesKitKind;
                        sk.SSnote = string.Empty;
                        sk.SSrtype = (int)salesKitRulesType;
                        sk.SSstartdate = st;
                        sk.SSstatus = true;
                        sk.SStitle = stitle;
                        sk.SStype = (int)salesKitType;
                        sk.SStagid = tagids;
                        sk.SSnote = note;
                        _dc.USalesKit.InsertOnSubmit(sk);
                        _dc.SubmitChanges();
                        msg = "新增活動成功!!";
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "AddManagerSalesKit", ex.ToString());
                msg = "新增活動失敗";
                id = Guid.Empty;
            }
            return msg;
        }

        public USalesKit GetManagerSalesKit(Guid id, Config.SalesKitKind salesKitKind)
        {
            USalesKit sk = null;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    sk = _dc.USalesKit.SingleOrDefault(s => s.SSkind == (int)salesKitKind && s.SSguid==id);
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerSalesKit", ex.ToString());
            }
            return sk;
        }

        public string SetManagerSalesKit(Guid id,string tagids, string stitle, string note, DateTime st, DateTime et)
        {
            string msg = string.Empty;

            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sk = _dc.USalesKit.SingleOrDefault(s => s.SSguid == id);
                    if (sk != null)
                    {
                        bool ischeck = true;
                        var sc = _dc.USalesKit.Where(s => s.SSkind == sk.SSkind && s.SSrtype == sk.SSrtype && s.SStype == sk.SStype && s.SSguid != id);
                        if (sc != null)
                        {
                            //檢查日期有沒有重覆
                            if (sc.Any(s => s.SSkind == (int)Config.SalesKitKind.HanselDiscount &&
                                ((s.SSstartdate < st && s.SSenddate > et) ||
                                    (s.SSstartdate > st && s.SSenddate > et && s.SSstartdate < et) ||
                                    (s.SSstartdate < st && s.SSenddate < et && s.SSenddate > st) ||
                                    (s.SSstartdate > st && s.SSenddate < et)
                                )
                               ))
                            {
                                msg = "活動更新失敗(501 活動區間重覆)!!";
                                ischeck = false;
                            }
                            else if (sc.Any(s => s.SStitle == stitle))
                            {
                                msg = "活動更新失敗(502 活動名稱重覆)!!";
                                ischeck = false;
                            }
                        }

                        if (ischeck)
                        {
                            sk.SStitle = stitle;
                            sk.SSstartdate = st;
                            sk.SSenddate = et;
                            sk.SStagid = tagids;
                            sk.SSnote = note;
                            _dc.SubmitChanges();
                            msg = "狀態變更成功!!(200)";
                        }
                    }
                    else
                        msg = "活動不存在(500)!";
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "SetManagerSalesKit", ex.ToString());
                msg = "活動更新失敗!!";
            }
            return msg;
        }

        public string UptManagerSalesKitStatus(Guid id)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sk = _dc.USalesKit.SingleOrDefault(s => s.SSguid == id);
                    if (sk != null)
                    {
                        sk.SSstatus = !sk.SSstatus;
                        _dc.SubmitChanges();
                        return "狀態變更成功!!";
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "UptManagerSalesKit", ex.ToString());
            }
           return "狀態變更失敗!!";
                    
        }

        public string DelManagerSalesKit(Guid id)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sk = _dc.USalesKit.SingleOrDefault(s => s.SSguid == id);
                    if (sk != null)
                    {
                        //所有關連先刪除
                        _dc.USalesKitHandsel.DeleteAllOnSubmit(sk.USalesKitHandsel);
                        _dc.USalesKit.DeleteOnSubmit(sk);
                        _dc.SubmitChanges();
                        return "活動刪除成功!!";
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "DelManagerSalesKit", ex.ToString());
            }
            return "活動刪除失敗!!";
        }


        public void GetManagerHanselList(Guid id, out List<USalesKitHandsel> handsellist)
        {
            handsellist = new List<USalesKitHandsel>();
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sk = _dc.USalesKitHandsel.Where(s => s.SHSSguid == id);
                    if (sk.Count() > 0)
                        handsellist = sk.ToList();
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetManagerHanselList2", ex.ToString());
            }
        }

        public string AddManagerHanselDetail(Guid ssguid, int shlimit, int shpoint)
        {
            string msg = string.Empty;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sk = _dc.USalesKitHandsel.Where(s => s.SHSSguid == ssguid);
                    //只放一個條件
                    //if (sk.Any(s => s.SHbottomlimit == shlimit))
                    //    msg = "條件新增錯誤(條件金額重覆)";
                    //else
                    //{
                    _dc.USalesKitHandsel.DeleteAllOnSubmit(sk);
                    _dc.SubmitChanges();
                    USalesKitHandsel us = new USalesKitHandsel();
                    us.SHbottomlimit = shlimit;
                    us.SHgiftprice = shpoint;
                    us.SHSSguid = ssguid;
                    _dc.USalesKitHandsel.InsertOnSubmit(us);
                    _dc.SubmitChanges();
                    //}
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "AddManagerHanselDetail", ex.ToString());
                msg = "條件新增錯誤!!";
            }
            return msg;
        }

        public string DelManagerHanselDetail(int shid)
        {
            string msg = string.Empty;
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sk = _dc.USalesKitHandsel.Where(s => s.SHid==shid);
                    _dc.USalesKitHandsel.DeleteAllOnSubmit(sk);
                    _dc.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "DelManagerHanselDetail", ex.ToString());
                msg = "條件刪除錯誤!!";
            }
            return msg;
        }

        public void GetHanselJudgement(ref Dictionary<int, int> judge)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sk = _dc.USalesKit.Where(s => s.SSkind==(int)Config.SalesKitKind.HanselDiscount && DateTime.Today>=s.SSstartdate && DateTime.Now<=s.SSenddate && s.SSstatus).OrderByDescending(s=>s.SSdatein).FirstOrDefault();
                    if (sk != null)
                    {
                        var su = sk.USalesKitHandsel;
                        foreach(var s in su)
                        {
                            judge.Add(s.SHbottomlimit, s.SHgiftprice);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetHanselJudgement", ex.ToString());
            }
        }

        public USalesKit GetCombo(long tagid)
        {
            try
            {
                using (_dc = new DataClassesMenDataContext(Config.connString))
                {
                    var sc = _dc.USalesKit.SingleOrDefault(s => s.SStagid == tagid.ToString()
                        && s.SSstatus
                        && s.SSstartdate <= DateTime.Now
                        && s.SSenddate >= DateTime.Now
                        && s.SSkind == (int)Config.SalesKitKind.ComboDiscount
                        );
                    return sc;
                }
            }
            catch (Exception ex)
            {
                _log.RecordExceptionFormat(_log.DBExceptionPath, "GetCombo", ex.ToString());
            }
            return null;
        }
    }
}
